#ifndef __NIGHT_SKY_SOCKET_H
#define __NIGHT_SKY_SOCKET_H

extern "C"
{
#include <netinet/in.h>
}
#include <string>
#include "common/closable.hpp"
#include "common/uncopyable.hpp"

namespace night_sky
{
  using namespace common;

  /**
   * accept したソケット．
   * 
   * @author krdlab
   */
  class AcceptedSocket : public Closable, public Uncopyable
  {
  private:
    volatile int m_socket;
    struct sockaddr_in m_from;

  public:
    AcceptedSocket(const int socket, const struct sockaddr_in& from);
    ~AcceptedSocket();

    std::string recvUntil(const std::string& eom="") const;
    bool send(const std::string& data) const;

    const struct sockaddr_in& getSockAddrIn() const;

    bool isClosed() const;
    void close();
  };

  /**
   * サーバサイドのソケット．
   * 
   * @author krdlab
   */
  class ServerSocket : public Closable, public Uncopyable
  {
  private:
    volatile int m_socket;

  public:
    ServerSocket();
    ~ServerSocket();

    bool init();

    bool bind(const int port) const;
    bool listen(const int backlog) const;

    AcceptedSocket* accept() const;

    bool isClosed() const;
    void close();
  };
}

#endif

